home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / easyproc2.lha / EasyProcess / hc / hc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  9.7 KB  |  476 lines

  1. /*
  2.  *    HC: device
  3.  *
  4.  */
  5.  
  6. #include "hc.h"
  7.  
  8. #define BTOC(x) ((void *)((ULONG)(x) << 2))
  9. #define CTOB(x) ((BPTR)((ULONG)(x) >> 2))
  10.  
  11. #define JOUR    5131
  12. #define MINUTE    1051
  13. #define TICK    (TICKS_PER_SECOND * 53)
  14.  
  15.  
  16. struct List ReadList;
  17. struct Process *Process;
  18. DB    struct DebugInfo dbi;
  19. DB    struct Library *DOSBase;
  20.  
  21.  
  22. void main (void)
  23. {
  24. DB    unsigned char temp[256];
  25.     struct DosPacket  *Packet = NULL;
  26.     struct DeviceNode *DevNode = NULL;
  27.     struct DeviceList *Volume = NULL;
  28.     struct FileLock *MasterLock[NUM_FILES] =
  29.     {
  30.         0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
  31.     };
  32.  
  33. DB    DOSBase = OpenLibrary ("dos.library", 0L);
  34. DB    if (NULL == DOSBase)
  35. DB        goto quit;
  36.  
  37.     /*
  38.      *    Get startup message.
  39.      */
  40.  
  41.     Process = (struct Process *)FindTask (0L);
  42.     Packet = WaitPacket (Process);
  43.  
  44.     /*
  45.      *    We need to fill the dn_Task to keep DOS from launching
  46.      *    us again and again.
  47.      */
  48.  
  49.     DevNode = BTOC (Packet->dp_Arg3);
  50.     DevNode->dn_Task = &Process->pr_MsgPort;
  51.  
  52.     /*
  53.      *    Open debug...
  54.      */
  55.  
  56. DB    dbi.HCProc = Process;
  57. DB    dbi.HCSig = AllocSignal (-1);
  58. DB    dbi.OutProc = StartProcess ("HC Debug Writer", Outer, &dbi, 10);
  59. DB    if (NULL == dbi.OutProc)
  60. DB        goto quit;
  61.  
  62.     /*
  63.      *    Create a bogus volume node.
  64.      */
  65.  
  66.     Volume = AllocMem (sizeof (struct DeviceNode), MEMF_PUBLIC);
  67.     if (Volume == NULL)
  68.         goto quit;
  69.  
  70.     CopyMem (DevNode, Volume, sizeof (struct DeviceNode));
  71.     Volume->dl_VolumeDate.ds_Days = JOUR;
  72.     Volume->dl_VolumeDate.ds_Minute = MINUTE;
  73.     Volume->dl_VolumeDate.ds_Tick = TICK;
  74.     Volume->dl_LockList = NULL;
  75.     Volume->dl_DiskType = DLT_VOLUME;
  76.  
  77.     /*
  78.      *    Get the locks.
  79.      */
  80.  
  81.     {
  82.         UWORD i;
  83.         for (i = 0; i < NUM_FILES; i++)
  84.         {
  85.             MasterLock[i] = AllocMem (sizeof (struct FileLock), MEMF_PUBLIC);
  86.             if (MasterLock[i] == NULL)
  87.             {
  88. DB                Say ("Can't get locks.\n");
  89.                 goto quit;
  90.             }
  91.             if (i)
  92.             {
  93.                 MasterLock[i]->fl_Link = CTOB(MasterLock[i - 1]);
  94.             }
  95.             else
  96.             {
  97.                 MasterLock[i]->fl_Link = NULL;
  98.             }
  99.             MasterLock[i]->fl_Key = i;
  100.             MasterLock[i]->fl_Access = SHARED_LOCK;
  101.             MasterLock[i]->fl_Task = &Process->pr_MsgPort;
  102.             MasterLock[i]->fl_Volume = CTOB(Volume);
  103.         }
  104.     }
  105.  
  106.     Volume->dl_LockList = CTOB (MasterLock[NUM_FILES-1]);
  107.  
  108.     /*
  109.      *    Setup data functions.
  110.      */
  111.  
  112.     if (!OpenData ())
  113.     {
  114. DB        Say ("Can't setup data.\n");
  115.         goto quit;
  116.     }
  117.  
  118.     /*
  119.      *    Get the serial device.
  120.      */
  121.  
  122.     if (!OpenSer ())
  123.     {
  124. DB        Say ("Can't get serial.\n");
  125.         goto quit;
  126.     }
  127.  
  128.     InitRead ();
  129.  
  130.     /*
  131.      *    Initialize the list of readers.
  132.      */
  133.  
  134.     NewList (&ReadList);
  135.  
  136.     /*
  137.      *    Everything went fine...
  138.      */
  139.  
  140.     Packet->dp_Res1 = DOSTRUE;
  141.     ReturnPacket (Packet, Process);
  142.  
  143.     /*
  144.      *    Process all request !
  145.      */
  146.  
  147. DB    Say ("Ready to process.\n\n");
  148.  
  149.     while (1)
  150.     {
  151.         Packet = WaitPacket (Process);
  152.         Packet->dp_Res1 = DOSTRUE;
  153.  
  154.         switch (Packet->dp_Type)
  155.         {
  156.             case ACTION_FINDUPDATE:
  157.             case ACTION_FINDINPUT:
  158.             case ACTION_FINDOUTPUT:
  159.             {
  160.                 if (Packet->dp_Arg2 == ROOT || BTOC(Packet->dp_Arg2) == MasterLock[ROOT])
  161.                 {
  162.                     struct FileHandle *File = BTOC(Packet->dp_Arg1);
  163.                     UWORD Name = WhichFile (BTOC (Packet->dp_Arg3));
  164.  
  165.                     if (Name > 0 && Name < NUM_FILES)
  166.                     {
  167. DB                        sprintf (temp, "Opening file `%s'.\n", GetName (Name) + 1);
  168.  
  169.                         File->fh_Arg1 = Name;
  170.                         File->fh_Port = (struct MsgPort *)DOSTRUE;
  171.                         File->fh_Type = &Process->pr_MsgPort;
  172.                     }
  173.                     else
  174.                     {
  175. DB                        strcpy (temp, "Opening non-existant file.\n");
  176.  
  177.                         Packet->dp_Res1 = DOSFALSE;
  178.                         Packet->dp_Res2 = ERROR_OBJECT_NOT_FOUND;
  179.                     }
  180.                 }
  181.                 else
  182.                 {
  183. DB                    strcpy (temp, "Can't open a file: invalid lock.\n");
  184.  
  185.                     Packet->dp_Res1 = DOSFALSE;
  186.                     Packet->dp_Res2 = ERROR_INVALID_LOCK;
  187.                 }
  188.                 break;
  189.             }
  190.             case ACTION_READ:
  191.             {
  192.                 Packet->dp_Res1 = GetData (Packet->dp_Arg1, (UBYTE *)Packet->dp_Arg2, Packet->dp_Arg3);
  193.  
  194. DB                sprintf (temp, "Reading %d byte(s) from file `%s' and getting %d.\n", (UWORD)Packet->dp_Arg3, GetName (Packet->dp_Arg1) + 1, (UWORD)Packet->dp_Res1);
  195.  
  196.                 if (Packet->dp_Res1 == -1)
  197.                 {
  198. DB                    Say ("Adding to waiter list.\n");
  199.  
  200.                     AddTail (&ReadList, &Packet->dp_Link->mn_Node);
  201.                     continue;
  202.                 }
  203.                 break;
  204.             }
  205.             case ACTION_WRITE:
  206.             {
  207. DB                sprintf (temp, "Writing %d byte(s) to file `%s'.\n", (UWORD)Packet->dp_Arg3, GetName (Packet->dp_Arg1) + 1);
  208.  
  209.                 Packet->dp_Res1 = PutData (Packet->dp_Arg1, (UBYTE *)Packet->dp_Arg2, Packet->dp_Arg3);
  210.                 break;
  211.             }
  212.             case ACTION_SEEK:
  213.             {
  214. DB                sprintf (temp, "Seeking file `%s'.\n", GetName (Packet->dp_Arg1) + 1);
  215.  
  216.                 Packet->dp_Res1 = 0;
  217.                 break;
  218.             }
  219.             case ACTION_LOCATE_OBJECT:
  220.             {
  221.                 if (Packet->dp_Arg1 == ROOT || BTOC(Packet->dp_Arg1) == MasterLock[ROOT])
  222.                 {
  223.                     UWORD FileNum = WhichFile (BTOC (Packet->dp_Arg2));
  224.  
  225.  
  226.                     if (FileNum < NUM_FILES)
  227.                     {
  228. DB                        sprintf (temp, "Locating file `%s'.\n", GetName (FileNum) + 1);
  229.  
  230.                         Packet->dp_Res1 = CTOB(MasterLock[FileNum]);
  231.                     }
  232.                     else
  233.                     {
  234. DB                        strcpy (temp, "Locating non-existant file.\n");
  235.  
  236.                         Packet->dp_Res1 = DOSFALSE;
  237.                         Packet->dp_Res2 = ERROR_OBJECT_NOT_FOUND;
  238.                     }
  239.                 }
  240.                 else
  241.                 {
  242. DB                    strcpy (temp, "Trying to locate from a file.\n");
  243.  
  244.                     Packet->dp_Res1 = DOSFALSE;
  245.                     Packet->dp_Res2 = ERROR_INVALID_LOCK;
  246.                 }
  247.                 break;
  248.             }
  249.             case ACTION_PARENT:
  250.             {
  251. DB                strcpy (temp, "Finding parent.\n");
  252.                 if (Packet->dp_Arg1 == ROOT || BTOC(Packet->dp_Arg1) == MasterLock[ROOT])
  253.                 {
  254.                     Packet->dp_Res1 = 0;
  255.                     Packet->dp_Res2 = 0 /* was: ERROR_DIR_NOT_FOUND */;
  256.                 }
  257.                 else
  258.                 {
  259.                     Packet->dp_Res1 = CTOB(MasterLock[ROOT]);
  260.                 }
  261.                 break;
  262.             }
  263.             case ACTION_COPY_DIR:
  264.             {
  265. DB                strcpy (temp, "Duplicating lock.\n");
  266.  
  267.                 Packet->dp_Res1 = Packet->dp_Arg1;
  268.                 break;
  269.             }
  270.             case ACTION_EXAMINE_OBJECT:
  271.             {
  272.                 struct FileLock *Lock = BTOC(Packet->dp_Arg1);
  273.                 struct FileInfoBlock *Info = BTOC(Packet->dp_Arg2);
  274.  
  275.                 if (Lock == ROOT)
  276.                     Lock = MasterLock[ROOT];
  277.  
  278. DB                sprintf (temp, "Examining file `%s'.\n", GetName (Lock->fl_Key));
  279.                 Info->fib_DiskKey = Lock->fl_Key;
  280.                 if (Lock == MasterLock[ROOT])
  281.                 {
  282.                     Info->fib_DirEntryType = 1;
  283.                     Info->fib_EntryType = 1;
  284.                 }
  285.                 else
  286.                 {
  287.  
  288.                     Info->fib_DirEntryType = -1;
  289.                     Info->fib_EntryType = -1;
  290.                 }
  291.                 strcpy (Info->fib_FileName, ShowName (Lock->fl_Key));
  292.                 Info->fib_Protection = 0;
  293.                 Info->fib_Size = DataSize (Lock->fl_Key);
  294.                 Info->fib_NumBlocks = Info->fib_Size;
  295.                 Info->fib_Date.ds_Days = JOUR;
  296.                 Info->fib_Date.ds_Minute = MINUTE;
  297.                 Info->fib_Date.ds_Tick = TICK;
  298.                 strcpy (Info->fib_Comment, GetComment (Lock->fl_Key));
  299.                 break;
  300.             }
  301.             case ACTION_EXAMINE_NEXT:
  302.             {
  303.                 struct FileInfoBlock *Info = BTOC(Packet->dp_Arg2);
  304.  
  305.                 if (Packet->dp_Arg1 == ROOT || BTOC(Packet->dp_Arg1) == MasterLock[ROOT])
  306.                 {
  307.                     if (Info->fib_DiskKey < NUM_FILES-1)
  308.                     {
  309. DB                        sprintf (temp, "Examining next file: `%s'.\n", GetName (Info->fib_DiskKey + 1) + 1);
  310.  
  311.                         Info->fib_DiskKey += 1;
  312.                         Info->fib_DirEntryType = -1;
  313.                         Info->fib_Protection = 0;
  314.                         Info->fib_EntryType = -1;
  315.                         strcpy (Info->fib_FileName, ShowName (Info->fib_DiskKey));
  316.                         Info->fib_Size = DataSize (Info->fib_DiskKey);
  317.                         Info->fib_NumBlocks = Info->fib_Size;
  318.                         Info->fib_Date.ds_Days = JOUR;
  319.                         Info->fib_Date.ds_Minute = MINUTE;
  320.                         Info->fib_Date.ds_Tick = TICK;
  321.                         strcpy (Info->fib_Comment, GetComment (Info->fib_DiskKey));
  322.                     }
  323.                     else
  324.                     {
  325. DB                        strcpy (temp, "No more entry.\n");
  326.  
  327.                         Packet->dp_Res1 = DOSFALSE;
  328.                         Packet->dp_Res2 = ERROR_NO_MORE_ENTRIES;
  329.                     }
  330.                 }
  331.                 else
  332.                 {
  333. DB                    strcpy (temp, "Invalid Examine next.\n");
  334.  
  335.                     Packet->dp_Res1 = DOSFALSE;
  336.                     Packet->dp_Res2 = ERROR_INVALID_LOCK;
  337.                 }
  338.                 break;
  339.             }
  340.             case ACTION_DISK_INFO:
  341.             {
  342.                 Packet->dp_Arg2 = Packet->dp_Arg1;
  343.             }
  344.             case ACTION_INFO:
  345.             {
  346.                 struct InfoData *Disk = BTOC (Packet->dp_Arg2);
  347.  
  348. DB                strcpy (temp, "Getting info about disk.\n");
  349.  
  350.                 Disk->id_NumSoftErrors = 0;
  351.                 Disk->id_UnitNumber = 0;
  352.                 Disk->id_DiskState = ID_VALIDATED;
  353.                 Disk->id_NumBlocks = 4096 * 5;
  354.                 Disk->id_NumBlocksUsed = 0;
  355.                 Disk->id_BytesPerBlock = 1;
  356.                 Disk->id_DiskType = ID_DOS_DISK;
  357.                 Disk->id_VolumeNode = CTOB (Volume);
  358.                 Disk->id_InUse = 0;
  359.                 break;
  360.             }
  361.             case ACTION_CURRENT_VOLUME:
  362.             {
  363. DB                strcpy (temp, "Finding current volume.\n");
  364.  
  365.                 Packet->dp_Res1 = CTOB(Volume);
  366.                 break;
  367.             }
  368.             case ACTION_INHIBIT:
  369.             {
  370.                 if (Packet->dp_Arg1 == DOSTRUE)
  371.                 {
  372. DB                    strcpy (temp, "Inhibiting on.\n");
  373.  
  374.                     CloseSer ();
  375.                 }
  376.                 else
  377.                 {
  378. DB                    strcpy (temp, "Inhibiting off.\n");
  379.  
  380.                     OpenSer ();
  381.                 }
  382.                 break;
  383.             }
  384.             case ACTION_FLUSH:
  385.             {
  386. DB                strcpy (temp, "Flushing device.\n");
  387.  
  388.                 FlushWriteSer ();
  389.                 break;
  390.             }
  391.             case ACTION_SET_FILE_SIZE:
  392.             {
  393. DB                sprintf (temp, "Truncating file `%s'.\n", GetName (Packet->dp_Arg1) + 1);
  394.  
  395.                 break;
  396.             }
  397.             case ACTION_END:
  398.             {
  399. DB                sprintf (temp, "Closing file `%s'.\n", GetName (Packet->dp_Arg1) + 1);
  400.  
  401.                 break;
  402.             }
  403.             case ACTION_FREE_LOCK:
  404.             {
  405. DB                strcpy (temp, "Freeing lock.\n");
  406.  
  407.                 break;
  408.             }
  409.             case ACTION_SET_PROTECT:
  410.             {
  411. DB                strcpy (temp, "Setting protection.\n");
  412.  
  413.                 break;
  414.             }
  415.             case ACTION_SET_COMMENT:
  416.             {
  417. DB                strcpy (temp, "Setting comment.\n");
  418.  
  419.                 break;
  420.             }
  421.             case ACTION_SET_DATE:
  422.             {
  423. DB                strcpy (temp, "Setting date.\n");
  424.  
  425.                 break;
  426.             }
  427.             case ACTION_DIE:
  428.             {
  429. DB                Say ("Arrrrrrgh !\n");
  430.                 goto quit;
  431.                 break;
  432.             }
  433.             default:
  434.             {
  435. DB                sprintf (temp, "Unknown packet type: %d.\n", (UWORD)Packet->dp_Type);
  436.  
  437.                 Packet->dp_Res1 = DOSFALSE;
  438.                 Packet->dp_Res2 = ERROR_ACTION_NOT_KNOWN;
  439.                 break;
  440.             }
  441.         }
  442.  
  443. DB        Say (temp);
  444.         ReturnPacket (Packet, Process);
  445.     }
  446.  
  447. quit:
  448.  
  449.     {
  450.         UWORD i = 0;
  451.         while (i < NUM_FILES)
  452.             if (MasterLock[i])
  453.                 FreeMem (MasterLock[i], sizeof (struct FileLock));
  454.     }
  455.     CloseSer ();
  456.     CloseData ();
  457. DB    if (dbi.OutProc)
  458. DB    {
  459. DB        KillProcesses ();
  460. DB        WaitProcesses ();
  461. DB    }
  462. DB    if (DOSBase)    CloseLibrary (DOSBase);
  463.  
  464.     Packet->dp_Res1 = DOSFALSE;
  465.     Forbid ();
  466.     ReturnPacket (Packet, Process);
  467. }
  468.  
  469.  
  470. DB    void Say (char *text)
  471. DB    {
  472. DB        strcpy (dbi.Msg, text);
  473. DB        Signal (dbi.OutProc, 1L << dbi.OutSig);
  474. DB        Wait (1L << dbi.HCSig);
  475. DB    }
  476.